package com.ssextjs.component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import com.ssextjs.data.Store;
import com.ssextjs.engine.ConfiguraionImpl;

public class PagingToolbar extends Component {
	public int start=0;
	public String MsgPattern="Displaying topics %d - %d of %d";
	public int limit = 30;
	public Store store;
	public int count = 0;
	public HashMap buttons;
	
	public PagingToolbar(int start,int limit,int count,Store store) {
		this.start=start;
		this.limit=limit;
		this.count=count;
		this.store=store;
		this.buttons=new HashMap();
		initButtons();
		defInit();
		tplInit();
	}
	
	public void initButtons() {
		int curpage = (int) Math.floor((double)start/(double)limit);
		if (curpage<=0) {curpage=1;}
		Button first = new Button("", "x-tbar-page-first", getPageUrl(0), "alert('This is first button');");
		Button prev = new Button("", "x-tbar-page-prev", getPageUrl(curpage-1), "alert('This is prev button');");
		Button next = new Button("", "x-tbar-page-next", getPageUrl(curpage+1), "alert('This is next button');");
		Button last = new Button("", "x-tbar-page-last", getPageUrl((int) Math.floor((double)count/(double)limit)), "alert('This is last button');");
		Button refresh = new Button("", "x-tbar-loading", getPageUrl(curpage), "alert('This is refresh button');");
		
		
		int maxpage = (int) Math.floor((double)count/(double)limit);
		
		mainopt.put("curpage",curpage);
		mainopt.put("maxpage",maxpage);
		if (curpage<=1) {
			first.setEnable(false);
			prev.setEnable(false);
		}
		if (curpage>=maxpage) {
			next.setEnable(false);
			last.setEnable(false);
		}
		this.buttons.put("first",first);
		this.buttons.put("prev",prev);
		this.buttons.put("next",next);
		this.buttons.put("last",last);
		this.buttons.put("refresh",refresh);
	}
	
	private String getPageUrl(int page) {
		if (page<=0) {page=1;}
		return this.store.getDataUrl()+"&page="+page;
	}
	
	public PagingToolbar setStart(int start) {
		this.start = start;
		return this;
	}
	
	public int getStart() {
		return this.start;
	}
	
	public PagingToolbar setMsgPattern(String msg) {
		MsgPattern=msg;
		return this;
	}
	
	public String getMsgPattern() {
		return this.MsgPattern;
	}
	
	public PagingToolbar setLimit(int l) {
		this.limit=l;
		return this;
	}
	
	public int getLimit() {
		return this.limit;
	}
	
	public PagingToolbar setStore(Store st) {
		this.store=st;
		return this;
	}
	
	public Store getStore() {
		return this.store;
	}
	
	public PagingToolbar setCount(int c) {
		this.count=c;
		return this;
	}
	
	public int getCount() {
		return this.count;
	}
	
	
	public void defInit() {
		/*mainopt.put("start",this.start);
		mainopt.put("limit",this.limit);
		mainopt.put("store",this.store);
		mainopt.put("count",this.count);
		mainopt.put("buttons",this.buttons);
		mainopt.put("MsgPattern",this.MsgPattern);
		mainopt.put("obj",this);*/
	}
	
	public void tplInit() {
		try {
			tpl = ConfiguraionImpl.getCfg().getTemplate("PagingToolbar.tpl");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getMsg() {
		
		return String.format(this.MsgPattern, start+1,start+limit,count);
	}

	
}
